<?php
// Gestion des langues
class Lang
{
  private $lang;
  private $LANG;
  private $errorReporting;
  private $noTranslated;
  private $suportedLang;

  public function __construct($lang='gb')
  {
    $this->errorReporting = true;
    $this->noTranslated = array();
    $this->suportedLang = arrayOfLang();
    $this->lang = $lang;
    if(!in_array($this->lang, $this->suportedLang))
      {
	$this->lang = 'gb';
      }
        
    global $CONF;
    include $CONF['lang_dir'].'/'.$this->lang.'.php';

	if(file_exists($CONF['lang_dir'].'/custom/'.$this->lang.'.php')){
		include $CONF['lang_dir'].'/custom/'.$this->lang.'.php';
	}

    $this->LANG = $LANG; 
  }

  public function setLang($lang)
  {
    $this->lang = $lang;
    global $CONF;
    include $CONF['lang_dir'].'/'.$this->lang.'.php';
  }

  public function getLang()
  {
    return $this->lang;
  }

  public function getNoTranslated()
  {
    return $this->noTranslated;
  }

  public function getNoTranslatedToBeTranslated()
  {
    if(empty($this->noTranslated))
      {
	return true;
      }
    else
      {
	$c = '';
	foreach($this->noTranslated as $n)
	  {
	    $c .= '$LANG[\''.$n[0].'\'] = \''.str_replace('_',' ',$n[0]).'\'; // You can use : %%'.implode('%% ; %%',array_keys($n[1]))."%%\n";
	  }
	echo $c;
	return;
      }
  }

  public function setErrorReporting($state=true)
  {
    $this->errorReporting = (bool)$state;
  }

  public function tr($word,$replace=array())
  {
    $trStr = '';
    if(isset($this->LANG[$word]))
      {
        $trStr = $this->LANG[$word];
		if($trStr === true) {
			$trStr = str_replace('_',' ',$word);
		}
		else {
          foreach($replace as $key => $val)
            {
              $trStr = str_replace('%%'.strtoupper($key).'%%', $val, $trStr);
            }
          }
      }
    elseif(isset($this->LANG[strtolower($word)]))
      {
        $trStr = $this->LANG[strtolower($word)];
		if($trStr === true) {
			$trStr = str_replace('_',' ',$word);
		}
		else {
          foreach($replace as $key => $val)
            {
              $trStr = str_replace('%%'.strtoupper($key).'%%', $val, $trStr);
            }        
          }
          
        if(!in_array($word, array_keys($this->noTranslated)))
          {
            $this->noTranslated[$word] = array($word, $replace);
          }
        
        if($this->errorReporting === true)
          {
            trigger_error('No traduction for the word : "'.$word.'" using "'.strtolower($word).'"');
          }

      }
    else
      {
        if(!in_array($word, array_keys($this->noTranslated)))
          {
            $this->noTranslated[$word] = array($word, $replace);
          }
        
        if($this->errorReporting === true)
          {
            trigger_error('No traduction for the word : "'.$word.'"');
          }
        
        $trStr = str_replace('_',' ',$word);
        if(!empty($replace))
          {
            $trStr .= ' ('.implode(' - ', $replace).')';
          }
      }

    return $trStr;
  }

  public function trTemplate($t)
  {
    $ttr = array();
    foreach($t as $u)
    {
    $ttr['LANG_'.$u] = $this->tr($u);
    }
    return $ttr;
  }
}
?>
